<template>
    <div class="app-container">
        <el-row :gutter="20">
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                label-width="68px">
                <el-col :span="15">
                    <!-- <el-form-item label="店铺编号" prop="shopId">
          <el-input v-model="queryParams.shopId" placeholder="请输入店铺编号" clearable @keyup.enter.native="handleQuery" />
        </el-form-item> -->
                    <!-- <el-form-item label="" prop="shopName">
          <el-input v-model="queryParams.shopName" placeholder="请输入店铺名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="" prop="outerPayId">
          <el-input v-model="queryParams.outerPayId" placeholder="请输入外部支付单号" clearable @keyup.enter.native="handleQuery" />
        </el-form-item> -->
                    <!-- <el-form-item label="线上订单号" prop="soId">
          <el-input v-model="queryParams.soId" placeholder="请输入线上订单号" clearable @keyup.enter.native="handleQuery" />
        </el-form-item> -->
                    <!-- <el-form-item label="款式编码" prop="iId">
          <el-input v-model="queryParams.iId" placeholder="请输入款式编码" clearable @keyup.enter.native="handleQuery" />
        </el-form-item> -->
                    <!-- <el-form-item label="" prop="skuId">
          <el-input v-model="queryParams.skuId" placeholder="请输入商品编码" clearable @keyup.enter.native="handleQuery" />
        </el-form-item> -->
                    <!-- <el-form-item label="" prop="outerOiId">
          <el-input v-model="queryParams.outerOiId" placeholder="请输入子订单" clearable @keyup.enter.native="handleQuery" />
        </el-form-item> -->
                    <!-- <el-form-item label="" prop="qty">
          <el-input v-model="queryParams.qty" placeholder="请输入数量" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="" prop="amount">
          <el-input v-model="queryParams.amount" placeholder="请输入总额" clearable @keyup.enter.native="handleQuery" />
        </el-form-item> -->
                    <el-form-item label="" prop="referrerName">
                        <!-- <el-input v-model="queryParams.referrerName" placeholder="请输入达人名称" clearable
                @keyup.enter.native="handleQuery" /> remote
                :remote-method="gettalentInfoList"-->
                        <el-select v-model="queryParams.referrerName" placeholder="请输入达人名称" clearable filterable>
                            <el-option v-for="(item, index) in this.referrerNameList" :key="index"
                                :label="item.referrerName" :value="item.referrerName" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="" prop="referrerId">
                        <el-input v-model="queryParams.referrerId" placeholder="请输入达人uid" clearable
                            @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item prop="productNo" label="">
                        <!-- <el-input v-model="nameItem.productNo"></el-input> -->
                        <el-select v-model="queryParams.productNo" clearable filterable remote :remote-method="getDatas"
                            placeholder="款式" style="width:150px">
                            <el-option v-for="item in options" :key="item" :label="item" :value="item">
                            </el-option>
                        </el-select>
                    </el-form-item>

                    <!-- <el-form-item label="" prop="price">
          <el-input v-model="queryParams.price" placeholder="请输入单价" clearable @keyup.enter.native="handleQuery" />
        </el-form-item> -->
                    <!-- <el-form-item label="" prop="sendDate">
          <el-date-picker clearable v-model="queryParams.sendDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择发货日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="" prop="sendDay">
          <el-date-picker clearable v-model="queryParams.sendDay" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择发货日期">
          </el-date-picker>
        </el-form-item> -->
                    <!-- <el-form-item label="" prop="payStartDate">
          <el-date-picker clearable v-model="queryParams.payStartDate" type="date" value-format="yyyy-MM-dd"
            placeholder="支付时间开始">
          </el-date-picker>
        </el-form-item>
        -
        <el-form-item label="" prop="payEndDay">
          <el-date-picker clearable v-model="queryParams.payEndDay" type="date" value-format="yyyy-MM-dd"
            placeholder="支付时间结束">
          </el-date-picker>
        </el-form-item> -->


                    <!-- <el-form-item label="" prop="payDay">
          <el-date-picker clearable v-model="queryParams.payDay" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择支付日期">
          </el-date-picker>
        </el-form-item> -->
                    <!-- <el-form-item label="" prop="chosenChannel">
          <el-input v-model="queryParams.chosenChannel" placeholder="请输入实发快递渠道" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item> -->
                    <!-- <el-form-item label="" prop="commission">
          <el-input v-model="queryParams.commission" placeholder="请输入佣金" clearable @keyup.enter.native="handleQuery" />
        </el-form-item> -->
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-form-item label="" prop="sendDateStart">
                            <el-date-picker clearable v-model="queryParams.sendDateStart" type="date"
                                value-format="yyyy-MM-dd" placeholder="寄样时间开始">
                            </el-date-picker>
                        </el-form-item>
                        -
                        <el-form-item label="" prop="sendDateEnd">
                            <el-date-picker clearable v-model="queryParams.sendDateEnd" type="date"
                                value-format="yyyy-MM-dd" placeholder="寄样时间结束">
                            </el-date-picker>
                        </el-form-item>
                    </el-form-item>
                </el-col>
            </el-form>
            <timeSearch @getDateArr="getDateArr" @changDay="changDay" :timeArr="timeArr" :dayActive="this.dayActive"
                :dayLimit="1095" />
        </el-row>
        <!-- <div class="desc">
        <timeSearch @getDateArr="getDateArr" @changDay="changDay" :timeArr="timeArr" :dayActive="30" :dayLimit="180" />
      </div> -->

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5" style="display:none">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['product:ods:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5" style="display:none">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['product:ods:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5" style="display:none">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['product:ods:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5" style="display:none">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['product:ods:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <div ref="tableDom">
            <el-table v-loading="loading" :maxHeight="odsList.length ? tableHeight : 'auto'" :data="odsList"
                @selection-change="handleSelectionChange">

                <!-- <el-table-column type="selection" width="55" align="center" /> -->
                <el-table-column label="合作款式" align="center" prop="productNo" />
                <el-table-column label="款式主图" align="center" prop="picUrl">
                    <!-- <template slot-scope="scope">
              <image-preview :src="scope.row.picUrl" :width="50" :height="50" />
            </template> -->
                    <template #default="scope">
                        <el-popover effect="light" trigger="click" placement="right" width="auto" v-if="scope.row.picUrl">
                            <template #default>
                                <img :src='pjtUrl + scope.row.picUrl'
                                    style="width: 800px;height: 600px;object-fit: contain;" lazy>
                            </template>
                            <template #reference>
                                <img :src='pjtUrl + scope.row.picUrl' style="object-fit: contain;width: 80px; height: 80px"
                                    lazy />
                            </template>
                        </el-popover>
                    </template>
                </el-table-column>

                <el-table-column label="销售件数" align="center" prop="qtyNum" />
                <el-table-column label="销售额" align="center" prop="amountNumStr" />
                <el-table-column label="销售占比" align="center" prop="amountPercentStr" />
                <el-table-column label="达人名称" align="center" prop="referrerName" />
                <el-table-column label="达人uid" align="center" prop="referrerId" />
            </el-table>
        </div>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改订单来源信息对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="店铺编号" prop="shopId">
                    <el-input v-model="form.shopId" placeholder="请输入店铺编号" />
                </el-form-item>
                <el-form-item label="店铺名称" prop="shopName">
                    <el-input v-model="form.shopName" placeholder="请输入店铺名称" />
                </el-form-item>
                <el-form-item label="外部支付单号" prop="outerPayId">
                    <el-input v-model="form.outerPayId" placeholder="请输入外部支付单号" />
                </el-form-item>
                <el-form-item label="线上订单号" prop="soId">
                    <el-input v-model="form.soId" placeholder="请输入线上订单号" />
                </el-form-item>
                <el-form-item label="款式编码" prop="iId">
                    <el-input v-model="form.iId" placeholder="请输入款式编码" />
                </el-form-item>
                <el-form-item label="商品编码" prop="skuId">
                    <el-input v-model="form.skuId" placeholder="请输入商品编码" />
                </el-form-item>
                <el-form-item label="子订单" prop="outerOiId">
                    <el-input v-model="form.outerOiId" placeholder="请输入子订单" />
                </el-form-item>
                <el-form-item label="数量" prop="qty">
                    <el-input v-model="form.qty" placeholder="请输入数量" />
                </el-form-item>
                <el-form-item label="总额" prop="amount">
                    <el-input v-model="form.amount" placeholder="请输入总额" />
                </el-form-item>
                <el-form-item label="达人名称" prop="referrerName">
                    <el-input v-model="form.referrerName" placeholder="请输入达人名称" />
                </el-form-item>
                <el-form-item label="达人uid" prop="referrerId">
                    <el-input v-model="form.referrerId" placeholder="请输入达人uid" />
                </el-form-item>
                <el-form-item label="单价" prop="price">
                    <el-input v-model="form.price" placeholder="请输入单价" />
                </el-form-item>
                <el-form-item label="发货日期" prop="sendDate">
                    <el-date-picker clearable v-model="form.sendDate" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择发货日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="发货日期" prop="sendDay">
                    <el-date-picker clearable v-model="form.sendDay" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择发货日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="支付时间" prop="payDate">
                    <el-date-picker clearable v-model="form.payDate" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择支付时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="支付日期" prop="payDay">
                    <el-date-picker clearable v-model="form.payDay" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择支付日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="实发快递渠道" prop="chosenChannel">
                    <el-input v-model="form.chosenChannel" placeholder="请输入实发快递渠道" />
                </el-form-item>
                <el-form-item label="佣金" prop="commission">
                    <el-input v-model="form.commission" placeholder="请输入佣金" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import { listTemplate, getTemplate, delTemplate, addTemplate, updateTemplate, addTemplateAndSecondry, addProNoColorSize, productNoLike } from "@/api/product/template";
import timeSearch from "./day.vue";
import { listOds, getOds, delOds, addOds, updateOds, sampleOrderTalentSkuDwdTotal, talentInfoList, sampleOrderTalentIidDwdTotal } from "@/api/sample/ods";

export default {
    components: { timeSearch },
    name: "Ods",

    data() {
        return {
            options: [],
            referrerNameList: [],
            dayActive: 2,
            timeArr: [{ value: '2', label: '昨天' }, { value: '7', label: '最近7天' }, { value: '30', label: '最近30天' }, { value: '365', label: '最近一年' }, { value: '1', label: '日' }],
            timeGap: { startTime: "", endTime: "" },
            pjtUrl: process.env.VUE_APP_BASE_API,

            tableHeight: '',
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 订单来源信息表格数据
            odsList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数

            queryParams: {

                pageNum: 1,
                pageSize: 10,
                shopId: null,
                shopName: null,
                shopStatus: null,
                outerPayId: null,
                soId: null,
                iId: null,
                skuId: null,
                outerOiId: null,
                qty: null,
                amount: null,
                referrerName: null,
                referrerId: null,
                price: null,
                sendDate: null,
                sendDay: null,
                payDate: null,
                payDay: null,
                chosenChannel: null,
                commission: null,
                payStartDate: null,
                payEndDay: null,
                sendDateStart: null,
                sendDateEnd: null,
                productNo: null,

            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                outerPayId: [
                    { required: true, message: "外部支付单号不能为空", trigger: "blur" }
                ],
                soId: [
                    { required: true, message: "线上订单号不能为空", trigger: "blur" }
                ],
                outerOiId: [
                    { required: true, message: "子订单不能为空", trigger: "blur" }
                ],
            }
        };
    },
    created() {


        var timeGap = JSON.parse(localStorage.getItem("timeGap"))
        if (timeGap) {

            this.dayActive = timeGap.dayActive
            this.timeGap = timeGap
        }

        if (this.$route.query.productNo) {

            this.$set(this.queryParams, "productNo", this.$route.query.productNo);
            // this.$set(this.queryParams, "referrerId", this.$route.query.ods.referrerId);
        }
        // this.getList();
        this.gettalentInfoList()
    },
    mounted() {
        var _this = this
        this.resizeTable()
        window.onresize = function () {
            _this.resizeTable()
        }
    },
    //记得清空  如果在keepalive中 则使用activated  deactivated
    destroyed() {
        // localStorage.removeItem("timeGap")
        window.onresize = null
    },
    methods: {
        /* 获取货号列表 */
        getDatas(key) {

            console.log(JSON.stringify(key));
            this.loading = true;
            // return
            productNoLike(key).then((res) => {
                this.loading = false;
                this.options = res.data;
                console.log(JSON.stringify(this.options));
            });


        },
        //获取达人列表
        gettalentInfoList(key) {
            talentInfoList({ referrerName: key }).then(response => {
                response.forEach(item => {
                    if (item !== null) {
                        this.referrerNameList.push(item)

                    }
                })
                //  = response
                console.log(JSON.stringify(this.referrerNameList));
            })
        },
        changDay(val) {
            this.dayActive = val;
            this.timeGap.startTime = val[0];
            this.timeGap.endTime = val[1];
        },
        getDateArr(value) {
            this.timeGap.startTime = value[0];
            this.timeGap.endTime = value[1];
            console.log(JSON.stringify(this.timeGap));
            const obj = { productNo: this.queryParams.productNo, referrerName: this.queryParams.referrerName, referrerId: this.queryParams.referrerId, sendDateStart: this.queryParams.sendDateStart, sendDateEnd: this.queryParams.sendDateEnd, pageNum: this.queryParams.pageNum, pageSize: this.queryParams.pageSize, payStartDate: this.timeGap.startTime, payEndDay: this.timeGap.endTime }
            sampleOrderTalentIidDwdTotal(obj).then(response => {
                console.log(JSON.stringify(response.rows));
                this.odsList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
            this.dayActive = 1
        },
        /** 查询订单来源信息列表 */
        getList() {
            this.loading = true;
            sampleOrderTalentIidDwdTotal(this.queryParams).then(response => {
                console.log(JSON.stringify(response.rows));
                this.odsList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                shopId: null,
                shopName: null,
                shopStatus: null,
                outerPayId: null,
                soId: null,
                iId: null,
                skuId: null,
                outerOiId: null,
                qty: null,
                amount: null,
                referrerName: null,
                referrerId: null,
                price: null,
                sendDate: null,
                sendDay: null,
                payDate: null,
                payDay: null,
                chosenChannel: null,
                commission: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.queryParams.payStartDate = this.timeGap.startTime
            this.queryParams.payEndDay = this.timeGap.endTime
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.queryParams = {}
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加订单来源信息";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getOds(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改订单来源信息";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateOds(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addOds(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除订单来源信息编号为"' + ids + '"的数据项？').then(function () {
                return delOds(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('product/ods/export', {
                ...this.queryParams
            }, `ods_${new Date().getTime()}.xlsx`)
        },
        // table自动高度
        returnTableHeight(ref, bottomOffset) {
            let height = null
            height = window.innerHeight - ref.getBoundingClientRect().top - bottomOffset
            return height
        },


        resizeTable() {
            this.$nextTick(() => {
                this.tableHeight = this.returnTableHeight(this.$refs.tableDom, 100)
            }, 0)
        },
    }
};
</script>
  